package com.validate.advocatevalidator;


import com.validate.annotation.*;
import com.validate.annotation.impl.NotNullControl;
import com.validate.annotation.impl.NotNullGroupControl;
import com.validate.annotation.impl.NullControl;
import com.validate.annotation.impl.NullGroupControl;

import java.lang.reflect.Field;
import java.util.HashMap;

/**
 * 校验工具初始化
 */
public class GuideInitialize {

	protected static HashMap<String,ValidatorsHandler> handlerHashMap = new HashMap<String, ValidatorsHandler>();

	protected static HashMap<String,ValidatorsGroupHandler> handlerGroupHashMap = new HashMap<String, ValidatorsGroupHandler>();

	/**
	 * 初始化加入相关控制器
	 */
	static {
		handlerHashMap.put(Null.class.getName(),new NullControl());
		handlerHashMap.put(NotNull.class.getName(),new NotNullControl());

		handlerGroupHashMap.put(NullGroup.class.getName(), new NullGroupControl());
		handlerGroupHashMap.put(NotNullGroup.class.getName(),new NotNullGroupControl());
	}

	/**
	 * 获取到注解的控制实现类
	 * @param annotationType
	 * @param f
	 * @return
	 */
	public static ValidatorsHandler[] validatorsHandlers(String annotationType ,Field f){
		ValidatorsHandler validatorsHandler = handlerHashMap.get(annotationType);
		if (validatorsHandler != null) {
			return new ValidatorsHandler[]{validatorsHandler};
		}
		ValidatorsGroupHandler validatorsClass= handlerGroupHashMap.get(annotationType);
		if (validatorsClass != null) {
			return validatorsClass.field(handlerHashMap,f);
		}
		return new ValidatorsHandler[]{};
	}


}

